/**
 * @summary 文章请求模块
 */

import req from '@/utils/request';
import * as URL from '@/constants/api';
// 文章首页
export function getArticleList(id) {
  return req({
    url: URL.ARTICLE_LIST,
    params: {
      channel_id: id,
      timestamp: Date.now(),
    }
  })
    .then(res => res.data.data.results)
    .catch(() => null);
}
// 文章详情页
export function getNewsList(id) {
  return req({
    url: URL.NEWS_LIST + `/${id}`,
  })
    .then(res => res.data.data)
    .catch(() => null);
}

// 文章点赞
export function likings(id) {
  return req({
    url: URL.LIKINGS,
    method: 'post',
    data: {
      target: id,
    }
  })
    .then(res => {
      // localStorage.setItem('token', `Bearer ${res.data.data.token}`);
      return res.data.message;
    })
    .catch(err => Promise.reject(err.response.data.message))
}


// 取消文章点赞
export function unLikings(id) {
  return req({
    url: URL.LIKINGS + `/${id}`,
    method: 'delete',
  })
    .then(res => {
      // localStorage.setItem('token', `Bearer ${res.data.data.token}`);
      return res.data?.message || '取消点赞成功';
    })
    .catch(err => Promise.reject(err.response.data?.message || '取消点赞失败'))
}


// 收藏文章
export function collections(id) {
  return req({
    url: URL.COLLECT,
    method: 'post',
    data: {
      target: id,
    }
  })
    .then(res => {
      // localStorage.setItem('token', `Bearer ${res.data.data.token}`);
      return res.data.message;
    })
    .catch(err => Promise.reject(err.response.data.message))
}

// 取消收藏文章
export function unCollections(id) {
  return req({
    url: URL.LIKINGS + `/${id}`,
    method: 'delete',
  })
    .then(res => {
      // localStorage.setItem('token', `Bearer ${res.data.data.token}`);
      return res.data.message;
    })
    .catch(err => Promise.reject(err.response.data.message))
}
